package com.xxl.dao.impl;

import com.xxl.dao.StudentDao;
import com.xxl.util.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;


public class StudentDaoImpl implements StudentDao {
    private Connection conn;
    private PreparedStatement preparedStatement;

    @Override
    public ResultSet getAll() {
        try {
            conn = new JDBCUtil().init();
            preparedStatement = conn.prepareStatement("SELECT studentId,studentName,classesName FROM student");
            return preparedStatement.executeQuery();
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        } catch (SQLException throwables) {
            System.out.println(throwables);
        }
        return null;
    }

    public static String getUUID(){
        UUID uuid= UUID.randomUUID();
        String str = uuid.toString();
        return str.replace("-", "");
    }

    @Override
    public boolean addStudent(String studentName, String classesName) {
        try {
            String uuid = getUUID();
            conn = new JDBCUtil().init();
            preparedStatement = conn.prepareStatement("insert into student (studentId,studentName,classesName) values (?,?,?)");
            preparedStatement.setString(1,uuid);
            preparedStatement.setString(2,studentName);
            preparedStatement.setString(3,classesName);
            return preparedStatement.executeUpdate()>=1?true:false;
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        } catch (SQLException throwables) {
            System.out.println(throwables);
        }finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                System.out.println(throwables);
            }
        }
        return false;
    }

    @Override
    public boolean updateStudent(String studentId, String studentName, String classesName){
        try {
            conn = new JDBCUtil().init();
            preparedStatement = conn.prepareStatement("update student set studentName = ? , classesName = ? where studentId = ?");
            preparedStatement.setString(1,studentName);
            preparedStatement.setString(2,classesName);
            preparedStatement.setString(3,studentId);
            return preparedStatement.executeUpdate()>=1?true:false;
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        } catch (SQLException throwables) {
            System.out.println(throwables);
        }finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                System.out.println(throwables);
            }
        }
        return false;
    }

    @Override
    public boolean delByStudentId(String studentId){
        try {
            conn = new JDBCUtil().init();
            preparedStatement = conn.prepareStatement("delete from student where studentId = ?");
            preparedStatement.setString(1,studentId);
            return preparedStatement.executeUpdate()>=1?true:false;
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        } catch (SQLException throwables) {
            System.out.println(throwables);
        }finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                System.out.println(throwables);
            }
        }
        return false;
    }

}
